home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7372 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Tradition or what?
  5. Date: Sun, 25 Feb 96 22:58:00 GMT
  6. Organization: none
  7. Message-ID: <825289080snz@genesis.demon.co.uk>
  8. References: <4g0elg$mdr@redstone.interpath.net> <Dn4wML.5LJ@undergrad.math.uwaterloo.ca> <824950522snz@genesis.demon.co.uk> <4gqdop$jfo@redstone.interpath.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4gqdop$jfo@redstone.interpath.net>
  15.            softbase@mercury.interpath.net
  16.            "Scott McMahan - Softbase Systems" writes:
  17.  
  18. >Speaking of bizarre return values, do people realize 0 is used to mean
  19. >both success, failure, and a valid return value in the standard
  20. >library!? All three in the same library!
  21.  
  22. I don't find that to be a great problem. The semantics of each function
  23. usually suggest quite naturally what 0 should mean.
  24.  
  25. >0 means success when an error code is returned. clock() is an
  26. >example -- since -1 means error, any other value is successful.
  27.  
  28. Right - when you consider what clock() does then 0 is a natural value
  29. for a successful operation.
  30.  
  31. >(I need to go back and check, but I think this is how it works.)
  32. >
  33. >0 means failure when a valid value is returned like a pointer from
  34. >malloc().
  35.  
  36. Functions that return pointers cannot return 0 - they return null pointers.
  37. Null pointers are routinely used to indicate failure or the absence of
  38. a value.
  39.  
  40. >Zero is also a valid value in strcmp which can't fail (unless you pass
  41. >it bogus strings and it crashes) -- 0 means equal from the set of less
  42. >than, equal, and greater than.
  43.  
  44. strcmp probably generates the greatest amount of discussion on this topic.
  45. However the way it is defined allows it to be implemented very efficiently
  46. on most archetectures and makes a lot of sense if you think of it in
  47. similar terms to subtraction (or difference) in maths. Algebraically
  48. a-b is negative is a<b, 0 if a==b and positive if a>b. That is also why
  49. it implements efficiently.
  50.  
  51. >No wonder C is confusing and error prone! Try defining a macro
  52. >to represent what 0 means in the stdlib! :)
  53.  
  54. There's no point in doing this - 0 is 0.
  55.  
  56. >I think this is the case against explicit tests against 0 in
  57. >if, while, etc expressions which decide what to do based on the
  58. >return value of a function. It's much more clear if there's no
  59. >explicit test against 0.
  60.  
  61. This statement seems pretty much unrelated to what you wrote previously.
  62. How is implicit comparison with 0 clearer than explicit comparison with 0.
  63. What you say above seems to indicate that explicit comparison with NULL,
  64. is the clearest approach where pointers results are concerned.
  65.  
  66. -- 
  67. -----------------------------------------
  68. Lawrence Kirby | fred@genesis.demon.co.uk
  69. Wilts, England | 70734.126@compuserve.com
  70. -----------------------------------------
  71.